package com.syf.config.swagger;


import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

/**
 * Swagger2 用做 API 文档的产出工具
 *
 * @ConfigurationProperties注解，主要是用来把properties或者yml配置文件转化为bean来使用,并从IOC容器中获取bean
 * @EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效
 *
 */
@Configuration
@EnableSwagger2 //开启Swagger2
@EnableConfigurationProperties(SwaggerProperties.class) //把使用 @ConfigurationProperties 的类进行了一次注入
public class SwaggerAutoConfiguration {

    private SwaggerProperties swaggerProperties;

    public SwaggerAutoConfiguration(SwaggerProperties swaggerProperties) { //注入SwaggerProperties类
        this.swaggerProperties = swaggerProperties;
    }


    @Bean
    public Docket docket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())  //api 信息的简介
                .select()
                //swagger2扫描配置文件中定义的包路径：swagger2:base-package: com.syf.controller   #
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())) //去扫描指定包下的文件(controller)
                .paths(PathSelectors.any()) //所有路径
                .build();

        // 安全的配置
        docket.securitySchemes(securitySchemes()) // 安全规则
                .securityContexts(securityContexts()); // 安全配置的上下问

        return docket;
    }

    /**
     * api 信息的简介（构建 api文档的详细信息函数）
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .contact(new Contact(swaggerProperties.getName(), swaggerProperties.getUrl(), swaggerProperties.getEmail()))
                .title(swaggerProperties.getTitle())  //"api文档"
                .description(swaggerProperties.getDescription()) //描述： "restful 风格接口"
                .version(swaggerProperties.getVersion())
                .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
                .build();

    }

    /**
     * 安全的规则配置
     *
     * @return
     */
    private List<SecurityScheme> securitySchemes() {
        return Arrays.asList(new ApiKey("Authorization", "Authorization", "Authorization"));
    }

    /**
     * 安全的上下问
     *
     * @return
     */
    private List<SecurityContext> securityContexts() {
        return Arrays.asList(new SecurityContext(
                Arrays.asList(new SecurityReference("Authorization", new AuthorizationScope[]{new AuthorizationScope("global", "accessResource")})),
                PathSelectors.any()
        ));
    }

}
